package com.hnd.springframerwork.beans.factory.support;

import com.hnd.springframerwork.beans.BeansException;
import com.hnd.springframerwork.beans.factory.config.BeanDefinition;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class SimpleInstantiationStrategy implements InstantiationStrategy {

    @Override
    public Object instantiate(BeanDefinition beanDefinition, Constructor ctor, Object... args) {
        Class beanClazz = beanDefinition.getBeanClazz();
        try {
            if (ctor != null) {
                return beanClazz.getDeclaredConstructor(ctor.getParameterTypes()).newInstance(args);
            }
            return beanClazz.newInstance();
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            throw new BeansException("bean实例化失败",e);
        }

    }
}
